package com.ulic.customerManApplication.util.time;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;

import com.ulic.customerManApplication.commonmodule.bo.BirthdayMessage;
import com.ulic.customerManApplication.commonmodule.bo.MailSignature;
import com.ulic.customerManApplication.mailmodule.service.MailService;
import com.ulic.customerManApplication.membermodule.bo.Member;
import com.ulic.customerManApplication.membermodule.service.MemberService;
import com.ulic.customerManApplication.membermodule.vo.MemberVO;
import com.ulic.customerManApplication.messagemodule.service.MessageService;
import com.ulic.customerManApplication.pager.PagerModel;
import com.ulic.customerManApplication.servlet.GetSpringBeanServlet;
import com.ulic.customerManApplication.util.mail.SendMail;
import com.ulic.customerManApplication.util.message.MessageUtil;

public class NFDFlightDataTimerTask extends TimerTask{
	@Override
	public void run() {
		MemberService memberService =  (MemberService)GetSpringBeanServlet.getBean("memberService");
	    MailService mailService = (MailService)GetSpringBeanServlet.getBean("mailService");
	    MessageService messageService = (MessageService)GetSpringBeanServlet.getBean("messageService");
//		PagerModel pm = memberService.getMembers(1);
		SendMail sendMail = new SendMail();
		MessageUtil messageUtil = new MessageUtil();
		Member member  = null;
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String today = sdf.format(now);
		MailSignature mailSignature = null;
		BirthdayMessage birthdayMessage = null;
		BirthdayMessage cellBirthdayMessage = null;
		try{
			for(Object object :memberService.getMembers(new MemberVO())){
				if(object instanceof Member){
					member = (Member)object;
					if(member.getBirthday()!= null && member.getBirthday().length()!=0){
						if(isBirthday(today, member.getBirthday())){
							if(member.getEmail()!= null && member.getEmail().length()!= 0){
								mailSignature = mailService.getMailSignature(member.getUser());
								String signature = mailSignature == null||mailSignature.getSignature()==null ? "" : "<br/><br/>"+new String(mailSignature
										.getSignature());
								birthdayMessage = mailService.getBirthdayMessageByType(member.getUser(),"MAIL");
								if(birthdayMessage!=null&&birthdayMessage.getMessage()!=null){
									sendMail.sendMails(member.getEmail(),"生日快乐",birthdayMessage.getMessage(),signature);
								}
							}
							if(member.getCellPhone()!= null && member.getCellPhone().length()!=0){
								cellBirthdayMessage = messageService.getBirthdayMessageByType(member.getUser(),"MESSAGE");
								if(cellBirthdayMessage!=null&&cellBirthdayMessage.getMessage()!=null){
									messageUtil.directSendMessage(member.getCellPhone(),cellBirthdayMessage.getMessage(), "130001");
								}
							}
						}
					}
				}
			}
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	public boolean isBirthday(String today,String birthday){//判断今天是否生日
		String[] todayStr = today.split("-");
		String[] birthdayStr = birthday.split("-");
		boolean flag = false;
		if(todayStr.length==3 && birthdayStr.length==3){
			int todayDay = Integer.parseInt(todayStr[1]);
			int todayMon = Integer.parseInt(todayStr[2]);
			int birthDay = Integer.parseInt(birthdayStr[1]);
			int birthMonth = Integer.parseInt(birthdayStr[2]);
			if( (todayMon== birthMonth) && (todayDay==birthDay)){
				flag = true;
			}
		}
		return flag;
	}

}
